<html>
<head><meta charset="utf-8"><title>Implementing a trait for all enums · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Implementing.20a.20trait.20for.20all.20enums.html">Implementing a trait for all enums</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="243309940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Implementing%20a%20trait%20for%20all%20enums/near/243309940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Wagner-Hall <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Implementing.20a.20trait.20for.20all.20enums.html#243309940">(Jun 20 2021 at 16:23)</a>:</h4>
<p>In response to feedback on <a href="https://github.com/rust-lang/rust/pull/81642">https://github.com/rust-lang/rust/pull/81642</a> - I'm trying to work out how to go about automatically implementing a trait for all enums (which meet certain trivially detectable properties) automatically...</p>
<p>From grepping around <code>rustc_interface</code>, it looks like the simplest way to go may be to teach the macro expander (in <code>rustc_expand</code>) that it should run a derive macro which only exists for the purposes of <code>rustc</code> and isn't exposed anywhere outside the compiler, even though no derive macro is written on the type... Does that sounds about right?</p>
<p>I also thought about having <code>rustc_ast_lowering</code> just pop out a second Item as a side-effect of lowering enums, but it looks like that code strongly assumes that one <code>Item</code> is lowered to exactly one <code>Item</code> (which is very reasonable!)</p>
<p>Is there an obviously better approach? Or any other prior art anyone can think of? I can only think of traits which are blanket impl'd (e.g. <code>Any</code>) but enums AFAIK don't have properties which would allow a blanket impl to target them, or traits which are impl'd on a finite known set of types (e.g. <code>ops::Add</code> for <code>u*</code> types), and not traits which are auto-impl'd for a subset of user-defined types...</p>



<a name="243312170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Implementing%20a%20trait%20for%20all%20enums/near/243312170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Implementing.20a.20trait.20for.20all.20enums.html#243312170">(Jun 20 2021 at 17:18)</a>:</h4>
<p>Even <a href="https://doc.rust-lang.org/nightly/core/marker/trait.DiscriminantKind.html">https://doc.rust-lang.org/nightly/core/marker/trait.DiscriminantKind.html</a> isn't impl'd only for enums, so...</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>